<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Interfaces
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Interfaces_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($cat, $uri)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('deleted', $uri['deleted']);
		$this->db->where('cat', $cat);
		if ($uri['q'] != '')
		{
			$this->db->like('name_vn', $uri['q']);
		}
        if ($uri['orderby'] == 'sort_order')
        {
            $this->db->where('status', 1);
        }
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->select('*, IF ('.sprintf('date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
        if ($uri['orderby'] == '')
        {
            $uri['orderby'] = 'id';
        }
        if ($uri['ordermode'] == '')
        {
            $uri['ordermode'] = 'desc';
        }
        $this->db->order_by($uri['orderby'] . ' ' . $uri['ordermode']);
		$query = $this->db->get('interfaces');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('id', $id);
		$query = $this->db->get('interfaces');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('interfaces');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function process($data, $id = '')
	{
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');	
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('interfaces', $data);
			write_log('update_interfaces', $data['name_vn']);
		}
		else
		{
			$this->db->insert('interfaces', $data);
			write_log('add_interfaces', $data['name_vn']);
		}
		return true;
	}

}

/* End of file interfaces_model.php */
/* Location: ./application/models/interfaces_model.php */